#Template API and Guide: http://guides.rubyonrails.org/rails_application_templates.html
#Add GEM List
#mongodid
gem 'mongoid'
gem 'mongoid-autoinc'
gem 'will_paginate_mongoid'
gem 'will_paginate'
gem 'pmap'
gem 'config'
#gem 'redis'

#trinidad
gem 'trinidad', :require=>nil


gem_group :development, :test do  
  gem 'pry'
  gem 'pry-remote'
  gem 'pry-nav'
  gem "rspec-rails"
  gem 'rspec'
end
#Create mongoid config 
generate(:"mongoid:config")
#Install Rspec
generate(:"rspec:install")
#Install Config
generate(:"config:install")

run "echo '--format documentation'>>.rspec"
run "rm -rf test"

#Create .rvm and .jruby rc
file(".jrubyrc") do
  <<-CODE
     compat.version=2.0
  CODE
end

#ruby_ver=ask("Ruby Version? [jruby-1.7.26]")
ruby_ver="jruby-1.7.26" 
file(".ruby-version") do
  <<-CODE
    #{ruby_ver}
  CODE
end


#Create Rake file
rakefile("db.rake") do
<<-TASK
namespace :db do
  desc 'Open a MongoDB console with connection parameters for the current Rails.env'
  task :console, [:session] => :environment do |t, args|
    session_name = args[:session] || :default

    config = Mongoid.clients[session_name]
    username = config[:username]
    password = config[:password]
    database = config[:database]

    session = Mongoid::Clients.with_name(session_name)
    host = session.cluster.servers.first.address.to_s

    args = []
    args << "--username=\#{username}" if username
    args << "--password=\#{password}" if password
    args << "--host=\#{host}"
    args << database
    
    exec 'mongo', *args
  end
end
TASK
end

#Create docker file
file("Dockerfile") do
<<-CODE
#**********************************************
FROM shaoyang/jrubyonrails:4.2.8-onbuild
MAINTAINER ShaoYang                                                           
CMD rails s trinidad -b 0.0.0.0
CODE
end

file(".dockerignore") do
<<-CODE
k8s-files
docker-swarm/*
docker-compose/*
.dockerignore
.git
public/assets
vendor/cache
tmp
log/*
.idea
config/*_local.yml
config/environments/local/*
*.swp
*.swo
.DS_Store
coverage
.secrets.env
docker-compose.yml
codeship-*.yml
CODE
end

file("dockercfg.yml") do
<<-CONFIG
repository: "#{app_name}"
dockerserver: "registry.cn-beijing.aliyuncs.com/shaoyang_builds"
default_tag: "latest"
servers: 
  - docker.io/shaoyang
  - quay.io/shaoyang_yu
  - registry.cn-beijing.aliyuncs.com/shaoyang_builds
CONFIG
end

file("./docker-compose/production/docker-compose.yml") do
<<-CONFIG
# Docker-compose 1.13 or great
version: '3'
services:
  web:
    image: #{app_name}
    restart: always
    environment:
      RAILS_ENV: production
      SECRET_KEY_BASE: #{`rake secret`}
#    env_file: .secrets.env
    ports:
      - "3000:3000"

    links:
      - db
#      - redis
    networks:
      - backend
    command: bundle exec trinidad --address 0.0.0.0

#    volumes:
#      - 

  db:
    image: mongo:latest
    volumes:
      - mongo-shared:/data/db
    networks:
      - backend

#    environment:
#      MYSQL_ALLOW_EMPTY_PASSWORD: 'true'

#  redis:
#    image: redis

volumes:
   mongo-shared:
     driver: local
networks:
  backend:
    driver: overlay
CONFIG

end

file("./docker-compose/development/docker-compose.yml") do
<<-CONFIG
# Docker-compose 1.13 or great
version: '3'
services:
  web:
    image: #{app_name}
    restart: always
    environment:
      RAILS_ENV: development
#    env_file: .secrets.env
    ports:
      - "3000:3000"

    links:
      - db
#      - redis
    networks:
      - backend
    command: rails s trinidad -b 0.0.0.0

#    volumes:
#      - 

  db:
    image: mongo:latest
    volumes:
      - mongo-shared:/data/db
    networks:
      - backend

#    environment:
#      MYSQL_ALLOW_EMPTY_PASSWORD: 'true'

#  redis:
#    image: redis

volumes:
   mongo-shared:
     driver: local
networks:
  backend:
    driver: overlay
CONFIG
end

run "ln -s docker-compose/development/docker-compose.yml ./"
run "ln -s docker-compose/development/docker-compose.development.yml ./"
run "ln -s docker-compose/development/docker-compose.production.yml ./"
